import java.util.Arrays;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: wwy
 * Date: 2024-11-22
 * Time: 12:42
 */
public class MyArrayList implements IList {
    public int[] array;//   0
    public int usedSize;
    public static final int DEFAULT_CAPACITY = 10;

    public MyArrayList() {
        this.array = new int[DEFAULT_CAPACITY];
    }

    @Override   //默认放最后
    public void add(int data) {
        if (isFull()) {
            grow();
        }
        this.array[usedSize] = data;
        this.usedSize++;
    }

    private void grow() {
        this.array = Arrays.copyOf(array, 2 * array.length);
    }

    @Override
    public boolean isFull() {
        return this.usedSize == array.length;
    }

    private void checkPos(int pos) throws PosIllegal {
        if (pos < 0 || pos > usedSize) throw new PosIllegal("Pos位置不合法！！");
    }

    @Override
    public void add(int pos, int data) {
        try {
            checkPos(pos);
            if (isFull()) {
                grow();
            }
            for (int i = this.usedSize - 1; i >= pos; i--) {
                this.array[i + 1] = this.array[i];
            }
            array[pos] = data;
            usedSize++;

        } catch (PosIllegal e) {
            e.printStackTrace();
        }

    }


    @Override
    public boolean contains(int toFind) {
        for (int i = 0; i < usedSize; i++) {
            if (array[i] == toFind) {
                return true;
            }
        }
        return false;
    }

    @Override
    public int indexOf(int toFind) {
        for (int i = 0; i < usedSize; i++) {
            if (array[i] == toFind) {
                return i;
            }
        }
        return -1;
    }

    private void checkPos2(int pos) throws PosIllegal {
        if (pos < 0 || pos >= usedSize) throw new PosIllegal("Pos位置不合法！！");
    }

    public void isEmpty() {
        if (this.usedSize == 0) {
            throw new EmptyException("kong");
        }
    }

    @Override
    public int get(int pos) {
        try {
            isEmpty();
            checkPos2(pos);
            return array[pos];
        } catch (EmptyException | PosIllegal e) {
            e.printStackTrace();
        }
        return -1;
    }

    @Override
    public void set(int pos, int value) {
        try {
            isEmpty();
            checkPos2(pos);
            array[pos]=value;
        } catch (EmptyException | PosIllegal e) {
            e.printStackTrace();
        }
    }

    @Override
    public void remove(int toRemove) {
        try {
            isEmpty();
           int pos=indexOf(toRemove);
           if(pos==-1){
               return;
           }
            for (int i = pos; i < usedSize-1; i++) {
                array[i]=array[i+1];
            }
            usedSize--;
        } catch (EmptyException e) {
            e.printStackTrace();
        }
    }

    @Override
    public int size() {
        return this.usedSize;
    }

    @Override
    public void clear() {
        for (int i = 0; i < this.usedSize; i++) {
            array[i]=0;
        }
        this.usedSize=0;
    }

    @Override
    public void display() {
        for (int i = 0; i < this.usedSize; i++) {
            System.out.print(array[i] + " ");
        }
    }
}

